package cn.edu.sdjzu.xg.kcsj.controller.basic;

import cn.edu.sdjzu.xg.kcsj.domain.DictItem;
import cn.edu.sdjzu.xg.kcsj.domain.Dictionary;
import cn.edu.sdjzu.xg.kcsj.service.DictItemService;
import cn.edu.sdjzu.xg.kcsj.service.DictionaryService;
import cn.edu.sdjzu.xg.kcsj.util.Condition;
import cn.edu.sdjzu.xg.kcsj.util.ControllerHelper;
import cn.edu.sdjzu.xg.kcsj.util.JSONUtil;
import cn.edu.sdjzu.xg.kcsj.util.Pagination;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.alibaba.fastjson.serializer.SerializerFeature;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.sql.SQLException;
import java.util.Collection;
import java.util.List;

@WebServlet("/dictItemFind")
public class DictItemFindCtrl extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws IOException {
        // 读取参数id
        JSONObject message = new JSONObject();
        String id_str = request.getParameter("id");
        Integer id_int = Integer.parseInt(id_str);
        try{
            DictItem dictItem = DictItemService.getInstance().find(id_int);
            String score_json = JSON.toJSONString(dictItem, SerializerFeature.DisableCircularReferenceDetect);
            response.getWriter().println(score_json);
        } catch (SQLException e) {
            message.put("message", "数据库操作异常");
            response.getWriter().println(message);
            // 在命令行打印异常信息在程序中出错的位置及原因
            e.printStackTrace();
        } catch (Exception e) {
            message.put("message", "网络异常");
            response.getWriter().println(message);
            // 在命令行打印异常信息在程序中出错的位置及原因
            e.printStackTrace();
        }
    }
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException {
        JSONObject message_jsonObj = new JSONObject();
        String req_jsonStr = JSONUtil.getJSON(request);
        JSONObject req_jsonObj = JSON.parseObject(req_jsonStr);
        if (req_jsonObj == null){
            message_jsonObj.put("message","未收到请求消息！");
            response.getWriter().println(message_jsonObj);
            return;
        }
        try {
            Pagination pagination = ControllerHelper.getPagination(req_jsonObj);
            List<Condition> conditionList = ControllerHelper.getConditions(req_jsonObj);
            Collection<DictItem> dictItems = DictItemService.getInstance().findAll(conditionList,pagination);
            message_jsonObj.put("totalNum",pagination.getTotalNum());
            message_jsonObj.put("totalPages",pagination.getTotalPages(pagination));
            message_jsonObj.put("data",dictItems);
            response.getWriter().println(message_jsonObj);
        } catch (SQLException e) {
            message_jsonObj.put("message","数据库操作异常");
            response.getWriter().println(message_jsonObj);
            e.printStackTrace();
        }catch (Exception e){
            message_jsonObj.put("message","网络异常");
            response.getWriter().println(message_jsonObj);
            e.printStackTrace();
        }
    }
}
